﻿namespace com.stifel.ServerBuildMgmt
{
    using System.Linq;
    using System.Reflection;
    using System.Xml;

    using com.stifel.Extensions.AssemblyExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class StifelEnvirons_Definition : BaseDefinition
    {
        #region Fields

        const string SITECOLUMNS = "Stifel-Environs.xml";

        #endregion Fields

        #region Constructors

        public StifelEnvirons_Definition( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~StifelEnvirons_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            loadList( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( getListDefn( ), false );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            StifelEnvirons c = null;
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<StifelEnvirons>( ),
                ContentTypeId = Item.GetContentTypeId<StifelEnvirons>( ),
                WebLevel = true,
                Group = "Stifel",
                Description = "Describes a server environment.",
                Fields = new FieldDefn[ ] {
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelEnvirons>(c.GetPropertySymbol(o=>o.DftEnvironment))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelEnvirons>(c.GetPropertySymbol(o=>o.EnvironmentDescrRequired))
                    }
                }.Distinct( ).ToList( )
            };
        }

        XmlNodeList getFieldDefinitionsAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "Fields" );
                fields = n.SelectNodes( "Field" );
            }
            return fields;
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = StifelEnvironsDao.ListName,
                Title = "Stifel Environments",
                Description = "Contains the server environments.",
                ContentTypes = new string[ ] {
                    Item.GetContentTypeName<StifelEnvirons>()
                }.ToList( )
            };
        }

        void loadList( )
        {
            StifelEnvironsDao dao = new StifelEnvironsDao( );
            dao.Clear( );
            dao.Add( new StifelEnvirons
            {
                Title = "Dev",
                DftEnvironment = false,
                EnvironmentDescrRequired = false
            } );
            dao.Add( new StifelEnvirons
            {
                Title = "QA",
                DftEnvironment = false,
                EnvironmentDescrRequired = false
            } );
            dao.Add( new StifelEnvirons
            {
                Title = "Production",
                DftEnvironment = true,
                EnvironmentDescrRequired = false
            } );
            dao.Add( new StifelEnvirons
            {
                Title = "Other",
                DftEnvironment = false,
                EnvironmentDescrRequired = true
            } );
        }

        #endregion Methods
    }
}